import wanAndroidApi from "@/core/storage/remote/WanAndroidApi.js"
import cacheKeys from "@/core/storage/local/CacheKeys.js"

export function login(username, password) {
	return wanAndroidApi.postKV({
		url: "user/login",
		data: {
			"username": username,
			"password": password
		},

	})
}

/**
 * 缓存账号信息
 */
export function setAccountInfoLocal(account, password) {
	let data = {
		"account": account,
		"password": password
	}
	//异步缓存
	uni.setStorage({
		key: cacheKeys.ACCOUNT_INFO,
		data: data,
		success() {
			console.debug("缓存账户成功");
		}
	});
	//同步缓存
	// uni.setStorageSync(cacheKeys.ACCOUNT_INFO, data);
}

/**
 * 获取本地缓存的账号信息
 * 异步获取的情况下 如果没有获取到 key 对应的 value  会进入fail回调
 */
export function getAccountInfoLocal(success) {
	//异步获取缓存
	uni.getStorage({
		// key: "aaa",
		key: cacheKeys.ACCOUNT_INFO,
		success: (response) => {
			console.debug("获取账户缓存成功：",response);
			success(response)
		},
		fail: (error) => {
			console.debug("获取账户缓存失败：",error);
		}
	})
	//同步获取缓存
	// let data = uni.getStorageSync("aaa");
	// console.debug("getStorageSync :",data.length === 0);
	// return uni.getStorageSync(cacheKeys.ACCOUNT_INFO);
}